﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using _MetalSlug.Mission;
using _MetalSlug.Soldiers.Units;

namespace _MetalSlug.Mementoes
{
    public class MissionMemento:IMemento
    {
        int level;
        public int Level
        {
            get { return level; }
        }

        int core;
        public int Core
        {
            get { return core; }
        }

        MapMemento map;
        public MapMemento Map
        {
            get { return map; }
        }

        HeroMemento hero;
        public HeroMemento Hero
        {
            get { return hero; }
        }

        List<SoldierMemento> soldiers;
        public List<SoldierMemento> Soldiers
        {
            get { return soldiers; }
        }

        float pivotPoint;

        public float PivotPoint
        {
            get { return pivotPoint; }
        }

        public MissionMemento(XmlElement Element)
        {
            level = int.Parse(Element.GetAttribute("Level"));
            core = int.Parse(Element.GetAttribute("Core"));
            pivotPoint = float.Parse(Element.GetAttribute("Pivot"));
            XmlElement elementMap = (XmlElement)Element.SelectSingleNode("./Map");
            map = new MapMemento(elementMap);

            XmlElement elementHero = (XmlElement)Element.SelectSingleNode("./Character");
            hero = new HeroMemento(elementHero);
            soldiers = new List<SoldierMemento>();
            XmlElement elementSoldiers = (XmlElement)Element.SelectSingleNode("./Soldiers");
            foreach(XmlElement elementSoldier in elementSoldiers.ChildNodes)
            {
                SoldierMemento soldier = new SoldierMemento(elementSoldier);
                soldiers.Add(soldier);
            }
        }

        public MissionMemento(MyMission RepresentMission, int GameCore)
        {
            core = GameCore;
            level = RepresentMission.Level;
            map = new MapMemento(RepresentMission.Map);
            hero = new HeroMemento(RepresentMission.Hero);
            pivotPoint = GameUtil.GamePivot.PivotPoint;
            soldiers = new List<SoldierMemento>();
            foreach(Enemy enemy in RepresentMission.GetListEnemy())
            {
                SoldierMemento soldierMemento = new SoldierMemento(enemy);
                soldiers.Add(soldierMemento);
            }
        }

        public XmlElement ToXML(XmlDocument Doc)
        {
            XmlElement Element = Doc.CreateElement("Mission");
            Element.SetAttribute("Level", Level.ToString());
            Element.SetAttribute("Core",Core.ToString());
            Element.SetAttribute("Pivot", PivotPoint.ToString());
            //Add Map
            Element.AppendChild(Map.ToXML(Doc));

            //Add Hero
            Element.AppendChild(Hero.ToXML(Doc));

            XmlElement EleSoldiers = Doc.CreateElement("Soldiers");
            foreach(SoldierMemento Soldier in Soldiers)
            {
                XmlElement eleSoldier = Soldier.ToXML(Doc);
                EleSoldiers.AppendChild(eleSoldier);
            }

            Element.AppendChild(EleSoldiers);
            return Element;
        }

        public IMemento GetEnemyMemento(int ID)
        {
            IMemento EnemyMemento = Soldiers.Where(Soldier=> Soldier.Id == ID).Take(1).SingleOrDefault();
            return EnemyMemento;
        }
    }
}
